/**
 * 权限相关的数据
 * Created by gcc on 2017/9/29.
 */

import Vue from "vue";
import  { Tooltip } from 'view-design'

// 页码相关
let page = {
  total: 1,
  startIndex: 0,
  stepNumber: 40,
  currentPage: 1
};

//页面状态相关
let status = {
  //查看图片
  modalImage: {
    isShow: false,
    url: ''
  },
  modalUserInfo:{
    isShow: false
  },
  modalBatchVerfiy:{
    isShow:false
  }
};

// 和网络交互相关的参数
let params = {
  //搜索条件
  contactEmail: '',
  firstName: '',
  startEndDate: [],
  orderBy: '',
  verifyInfo:{
    userId:"",
    userName:"",
    state:0,
    refusedReason:""
  },
  selectedCouponDefines:[]
};

// 页面的数据源
let data = {
  loginTypeDict: [],
  userList: [],
  userExtendInfo:{
    id:"",
    facebook:"",
    whatsapp:"",
    twitter:"",
    instagram:"",
    source:"",
    inviteCode:"",
    useType:0,
    useContent:"",
    createtime:0,
    isPushMessage:0,
    phoneCode:0,
    refusedReason:"",
    loginWay:"",
    contactEmail:""
  },
  dateRange: {
    disabledDate (date) {
      return date && date.valueOf() > Date.now();
    },
    shortcuts: [
      {
        text: '清空时间',
        value () {
          return [];
        }
      },
      {
        text: '最近一周',
        value () {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
          return [start, end];
        }
      },
      {
        text: '最近一个月',
        value () {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
          return [start, end];
        }
      },
      {
        text: '最近三个月',
        value () {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
          return [start, end];
        }
      }
    ]
  },
  selectedItems:[],
  couponDefineList: []
};

let tableTitles=function (self) {
  return [
    {
      type: 'selection',
      align: 'center',
      fixed: 'left',
      width: 60
    },
 {
      type: 'index',
      title: '#',
      width: 60,
      key: 'index',
      fixed: 'left',
      align: 'center',
    },
    {
      title: '头像',
      key: 'avatar',
      width: 80,
      align: 'center',
      render: (h, params) => {
        {
          return h('div', [h('Img', {
            attrs: {src: params.row.avatar, height: 50},
            on: {
              click: () => {
                status.modalImage.url = params.row.avatar;
                status.modalImage.isShow = true;
              }
            }
          })]);
        }
      }
    },
    {
      title: '用户名',
      key: 'userName',
      align: "center",
      render: (h, params) => {
        let username = Vue.prototype.$tiyHideUsername(params.row.fullName);
        return h('Tooltip',{props: {placement: 'top',theme:'dark',transfer:true,maxWidth:900}},[
            h('div',username),
            h('div',{slot:'content'},
             [
               h('div',{style:{width:'100%',padding: '1rem 1rem',}},[
              h('Row',{style:{'margin-top':'8px'}},[h('i-col',{props:{span:24}},[h('span','用户姓名 :'+params.row.firstname + ' ' + params.row.lastname)])]),
              h('Row',{style:{'margin-top':'8px'}},[h('i-col',{props:{span:24}},[h('span','FACEBOOK :'+params.row.facebook)])]),
              h('Row',{style:{'margin-top':'8px'}},[h('i-col',{props:{span:24}},[h('span','INSTAGRAM :'+params.row.instagram)])]),
              h('Row',{style:{'margin-top':'8px'}},[h('i-col',{props:{span:24}},[h('span','联系邮箱 :'+params.row.contactEmail)])]),
              h('Row',{style:{'margin-top':'8px'}},[h('i-col',{props:{span:24}},[h('span','手机号 :'+params.row.phoneCode)])]),
              h('Row',{style:{'margin-top':'8px'}},[h('i-col',{props:{span:24}},[h('span','客户来源 :'+params.row.source)])]),
              h('Row',{style:{'margin-top':'8px'}},[h('i-col',{props:{span:24}},[h('span','邀请码 :'+params.row.inviteCode)])]),
              h('Row',{style:{'margin-top':'8px'}},[h('i-col',{props:{span:24}},[h('span','是否允许推送 :'+(params.row.isPushMessage==1?'是':'否'))])]),
               ])
            ]
            )
          ])
      }
    },
   {
      title: '国家',
      key: 'country',
      width: 100,
      align: 'center'
    },
    {
      title: '注册方式',
      key: 'thirdParty',
      width: 100,
      align: 'center',
      render: (h, params) => {
        var way = '邮箱登录';
        data.loginTypeDict.forEach(function (item) {
          if (item.value == params.row.thirdParty) {
            way = item.name;
          }
        });
        return h('div', way);
      }
    },

  {
      title: '注册时间',
      width: 160,
      key: 'registertime',
      align: 'center',
      render: (h, params) => {
        return h('div', self.$tiyBJTime(params.row.registertime));
      }
    },
    {
      title: '操作',
      key: 'action',
      width: 300,
      align:'center',
      render: (h, params) => {
        return h('div',
          [
        //     !self.$authority(self.tRole.USER_3_204)?h("div",""):h('Button', {
        //   props: {type: 'primary', size: 'small'}, style: {marginRight: '16px'},
        //   on: {
        //     click: () => {
        //       self.allotUserShow(params.index);
        //     }
        //   }
        // }, '匹配目标用户'),
        !self.$authority(self.tRole.USER_3_201)?h("div",""):h('Button', {
          props: {type: 'primary', size: 'small'}, style: {marginRight: '16px'},
          on: {
            click: () => {
              self.showVerifyUser(params.index);
            }
          }
        }, '查看信息并审核')
      ]);
    }
  }
  ]
  };

let couponDefineTable = function(self){
  return [
    {
      type: 'selection',
      width: 55,
      fixed: 'left',
      align: 'center',
    },
    {
      type: 'index',
      width: 55,
      align: 'center',
      title: '#',
    },
    {
      title: '优惠券标题',
      key: 'title',
      width: 130,
      render:(h, params) => {
        if(self.$authority("COUPONS-1-204")){
          return (<div><a href="javascript:void(0);" onClick={() => {self.showCouponModelInfo(params.row.id);}}>{params.row.title}</a></div>);
        } else {
          return (<div>{params.row.title}</div>);
        }
      }
    },
    {
      title: '优惠券类型',
      key: 'type',
      width: 120,
      align: 'center',
      render: (h, params) => {
        let couponTypeDict = self.couponTypeDict;
        let type = params.row.type;
        let typeItem = couponTypeDict.filter(item=>item.value===type);
        let isPrimary = type===200;
        return (
          <div>
            <tag color={isPrimary?'green':'magenta'}>{isPrimary?'折扣券':'现金抵扣券'}</tag>
          </div>
        );
      },
    },
    {
      title: '启用状态',
      key: 'state',
      width: 110,
      render: (h, params) => {
        let isOpened = params.row.state === 0;
        let statusName = isOpened ? '生效中' : '已禁用';
        return (
          <div>
            <badge status={isOpened ? 'processing' : 'default'} text={statusName}/>
          </div>
        );
      },
      align: 'center'
    },
    {
      title: '折扣',
      key: 'discount',
      align: 'center',
      width: 180,
      render: (h, params) => {
        let discountValue = "";
        let couponType = params.row.type;
        if(couponType === 100){
          discountValue = '$'+params.row.faceValue;
        }else if(couponType == 200){
          discountValue = (100-params.row.discountRate*100).toFixed(1)+"%";
        }
        return h('div','满 $'+params.row.threshold+' 减 '+ discountValue)
      }
    },
  ]
};

let role = {
  userAddress: false,
  userCart: false,
  userOrder: false,
  userRecord: false,
  userSearch: false,
  userExport: false,
  userSearchAll: true,
};

export  default  {
  tPage: page,
  tStatus: status,
  tParams: params,
  userTableTitles: tableTitles,
  couponDefineTable:couponDefineTable,
  tData: data,
  tRole: role
};
